package com.easylinkin.linkappapi.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.quality.dto.QualityDrawingDTO;
import com.easylinkin.linkappapi.quality.entity.QualityDrawing;
import com.easylinkin.linkappapi.quality.mapper.QualityDrawingMapper;
import com.easylinkin.linkappapi.quality.service.QualityDrawingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import java.util.Optional;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 闸机表 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class QualityDrawingServiceImpl extends ServiceImpl<QualityDrawingMapper, QualityDrawing> implements QualityDrawingService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param qualityDrawing
   */
  @Override
  public void insert(QualityDrawing qualityDrawing) {
    /**
     * 验证重复
     */
    this.checkExist(qualityDrawing);
    //设置基本属性
    this.setBase(qualityDrawing);
    this.save(qualityDrawing);
  }

  /**
   * 根据id编辑
   * @param qualityDrawing
   */
  @Override
  public void updateOne(QualityDrawing qualityDrawing) {
    /**
     * 验证重复
     */
    this.checkExist(qualityDrawing);
    //设置基本属性
    this.setBase(qualityDrawing);
    this.updateById(qualityDrawing);
  }

  @Override
  public IPage<QualityDrawingDTO> queryListByPage(RequestModel<QualityDrawing> requestModel) {
    Page page = requestModel.getPage();
    QualityDrawing qualityDrawing = requestModel.getCustomQueryParams();
    //项目id 过滤
    qualityDrawing.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    IPage<QualityDrawingDTO> qualityDrawingIPage = baseMapper.queryListByPage(page, qualityDrawing);
    return qualityDrawingIPage;
  }

  /**
   * 验证重复
   */
  private void checkExist(QualityDrawing qualityDrawing) {
    QueryWrapper<QualityDrawing> queryWrapper = new QueryWrapper<>();
    //设置判断重复条件
    queryWrapper.eq("tenant_id_",linkappUserContextProducer.getNotNullCurrent().getTenantId())
        .eq("name_",qualityDrawing.getName())
        .eq("position_id_",qualityDrawing.getPositionId());
    //编辑的时候存在id
    Optional.ofNullable(qualityDrawing.getId()).ifPresent(id -> queryWrapper.ne("id",qualityDrawing.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该图纸已存在");
    }
  }

  /**
   * 设置基本属性
   * @param qualityDrawing
   */
  private void setBase(QualityDrawing qualityDrawing) {
    qualityDrawing.setModifyTime(new Date());
    qualityDrawing.setModifyId(linkappUserContextProducer.getCurrent().getId());
    //没有id就是新增,有就是编辑
    if (null == qualityDrawing.getId()){
      qualityDrawing.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      qualityDrawing.setCreateTime(new Date());
      qualityDrawing.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    }
  }
}
